package com.monkeygroup.mygoplus.common.utils;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Point;
import android.provider.MediaStore;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 功能说明:图像操作类
 * 作    者:
 * 创建日期:2015/11/4 13:03
 * 所属项目:通用1.0
 */
public class ImageTools {
	
	public static Point getFileSize(File source) throws IOException{
		BitmapFactory.Options opts= new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(source.toString(),opts);
		Point p=new Point(opts.outWidth, opts.outHeight);
		return p;
	}

    public static byte[] readStream(String filepath) throws Exception {  
         
        BitmapFactory.Options opts= new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(filepath,opts);
		 
		int width = opts.outWidth;
		int height = opts.outHeight;

		int k=1; 
		int m = width < height ? width : height;
		if (m > 400) {
			// 宽比高大
			k=(int) (m / 400);
		}
		if(k==0){
			k=1;
		}
		opts.inSampleSize=k;
		opts.inJustDecodeBounds = false;
		
		 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		 Bitmap bitmap = BitmapFactory.decodeFile(filepath.toString(),opts);
		bitmap.compress(CompressFormat.JPEG, 90, outStream);
        byte[] data = outStream.toByteArray();  
        bitmap.recycle();
        outStream.close();  
        return data;  
  
    }     
	
	public static boolean zoomPic(Context context,File source, File toFile) {
		if (toFile.exists()) {
			toFile.delete();
		}
		Bitmap bitmap=null;
		FileOutputStream out =null;
		boolean flag = true;
		try {
			// toFile.createNewFile();
			out = new FileOutputStream(toFile);
			BitmapFactory.Options opts= new BitmapFactory.Options();
	        opts.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(source.toString(),opts);
			 
			int width = opts.outWidth;
			int height = opts.outHeight;

			int k=1; 
			int m = width < height ? width : height;
			if (m > 400) {
				// 宽比高大
				k=(int) (m / 400);
			}
			if(k==0){
				k=1;
			}
			opts.inSampleSize=k;
			opts.inJustDecodeBounds = false;
			bitmap = BitmapFactory.decodeFile(source.toString(),opts);
			bitmap.compress(CompressFormat.JPEG, 90, out);
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}finally{
			if(out!=null){
				try {
					out.flush();
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(bitmap!=null){
				bitmap.recycle();
			}
		}
		return flag;
	}


	public static boolean zoomPic(Context context,Bitmap bitmap, File toFile) {
		if (toFile == null || toFile.exists()) {
			toFile.delete();
		}
		if (bitmap == null) {
			return false;
		}

		boolean flag = true;
		try {
			// toFile.createNewFile();
			FileOutputStream out = new FileOutputStream(toFile);
			int width = bitmap.getWidth();
			int height = bitmap.getHeight();

			int m = width < height ? width : height;
			if (m > 500) {
				// 缩放
				Matrix matrix = new Matrix();
				float k = 1.0f;// 缩放率
				// 宽比高大
				k = 400f / m;

				matrix.postScale(k, k);
				Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width,
						height, matrix, true);
				newbmp.compress(CompressFormat.JPEG, 90, out);
				out.flush();
				out.close();
				newbmp.recycle();
			} else {
				bitmap.compress(CompressFormat.JPEG, 90, out);
				out.flush();
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}

	/**
	 * 取相册最后一张
	 * 
	 * @param activity
	 * @return
	 */
	public static File getLastImageFromAlbum(Activity activity) {
		try {
			File re = null;
			String[] proj = { MediaStore.Images.Media.DATA };
			Cursor cursor = activity.getContentResolver().query(
					MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, null,
					null, MediaStore.Images.Media.DATE_MODIFIED + " desc");
			if (cursor != null && cursor.moveToFirst()) {
				String path = cursor.getString(0);
				re = new File(path);
			}
			cursor.close();
			return re;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
